package com.and.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.and.constant.Constant;
import com.and.dao.AccountDao;
import com.and.dao.IValidator;
import com.and.dao.impl.AccountDaoImpl;
import com.and.dao.impl.Validator;
import com.and.main.R;

/**
 * @Title: LoginUI.java
 * @Package com.and.ui
 * @Description: 登录页面
 * @author hanchunping
 * @date 2010-7-5 下午04:54:52
 * @version V1.0
 */
public class LoginUI extends Activity implements RadioGroup.OnCheckedChangeListener{

	/** Called when the activity is first created. */

	private ProgressDialog mProgressDialog = null;
	// 用户名
	private EditText username = null;
	// 密码
	private EditText password = null;
	// 是否自动登录
	private CheckBox isloginauto = null;
	// radio
	private RadioGroup rgroup = null;
	// 登录按钮
	private Button loginBtn = null;
	// 清空按钮
	private Button clearBtn = null;
	private int roleId;
	private IValidator vr;
	private AccountDao ad;
	private SharedPreferences.Editor editor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		// showDialog(Constant.DIALOG_PROCESS);
		init();
	}

	/**
	 * 
	 * @Title: init
	 * @Description: 初始化 各个控件
	 */
	private void init() {
		this.username = (EditText) findViewById(R.id.username);
		this.password = (EditText) findViewById(R.id.password);
		this.isloginauto = (CheckBox) findViewById(R.id.isautologin);
		this.rgroup = (RadioGroup) findViewById(R.id.radiogroup);
		this.loginBtn = (Button) findViewById(R.id.loginBtn);
		this.clearBtn = (Button) findViewById(R.id.clearBtn);
		this.loginBtn.setOnClickListener(loginClickListener);
		this.clearBtn.setOnClickListener(clearClickListener);
		vr = new Validator();
		ad = new AccountDaoImpl();
		// 获取SharedPreferences数据
		SharedPreferences sp = getSharedPreferences(Constant.ACCOUNT_FILE, 0);
		Boolean bl = sp.getBoolean(Constant.ISLOGINAUTO, false);
		if (bl) {
			this.isloginauto.setChecked(true);
		} else {
			this.isloginauto.setChecked(false);
		}
		this.roleId = (this.rgroup.getCheckedRadioButtonId() == R.id.client_1) ? 0
				: 1;
	}

	/**
	 * 登录监听
	 */
	private OnClickListener loginClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			// 判断用户名和密码是否正确
			final String name = username.getText().toString();
			final String psd = password.getText().toString();
			// 验证通过
			if (vr.validate(name, null) && vr.validate(psd, null)) {
				showDialog(Constant.DIALOG_PROCESS);
				new Thread() {
					public void run() {
						try {
							// 验证用户
							boolean ln = ad.login(name, psd, roleId);
							if (ln) {
								editor = getSharedPreferences(
										Constant.ACCOUNT_FILE, 0).edit();
								editor.putString(Constant.ACCOUNT, name);
								editor.putString(Constant.PASSWORD, psd);
								if (isloginauto.isChecked()) {
									editor.putBoolean(Constant.ISLOGINAUTO,
											true);
								} else {
									editor.putBoolean(Constant.ISLOGINAUTO,
											false);
								}
								Thread.sleep(5000);
								Intent intent = new Intent(LoginUI.this,
										FriendList.class);
								startActivity(intent);
								editor.commit();
								finish();
							} else {
								showDialog(Constant.DIALOG_LOGIN_FAIL_MESSAGE);
							}
							mProgressDialog.dismiss();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}.start();
			} else {
				showDialog(Constant.DIALOG_LOGIN_FAIL_MESSAGE);
			}

		}

	};
	/**
	 * 清空监听
	 */
	private OnClickListener clearClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

		}

	};

	/***************************************************************************
	 * 重新Dialog
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case Constant.DIALOG_LOGIN_FAIL_MESSAGE:
			return new AlertDialog.Builder(this).setTitle(
					R.string.alert_dialog_login_fail_msg).setPositiveButton(
					R.string.alert_dialog_ok,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub

						}
					}).create();
		case Constant.DIALOG_PROCESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setIcon(R.drawable.stat_happy);
			mProgressDialog.setMessage(getString(R.string.loading));
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setIndeterminate(true);
			return mProgressDialog;
		}
		return null;
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {

		roleId = (group.getCheckedRadioButtonId() == R.id.client_1) ? 0 : 1;

	}
}
